package com.web.control;

import com.web.dao.DepartmentDao;
import com.web.dao.EmployeeDao;
import com.web.pojo.Department;
import com.web.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;

@Controller
public class Employees {
    @Autowired
    EmployeeDao employeeDao;
    @Autowired
    DepartmentDao departmentDao;

    //    查询所有员工
    @RequestMapping("/emps")
    public String allemps(Model model) {
        Collection<Employee> emps = employeeDao.getALL();
//        employeeDao.test();
        model.addAttribute("emps", emps);
        return "emp/list";
    }

    //    跳转到增加员工页面
    @RequestMapping("/emp/add")
    public String add(Model model) {
//        展示部门信息
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments", departments);
        return "emp/add";
    }

    //    增加员工
    @RequestMapping("/emp/addemp")
    public String addepms(Employee employee) {
        System.out.println(employee);
        employeeDao.save(employee);
        return "redirect:/emps";
    }

    //    跳转到修改页面要携带数据restful风格不用？id=1这种老方式
//@RequestParam("id")写了传不上去页面
    @RequestMapping("/emp/toUpdate/{id}")
    public String toUpdate(@PathVariable("id") Integer id, Model model) {
        Employee employeeById = employeeDao.getEmployeeById(id);
        System.out.println(employeeById);
        model.addAttribute("employee", employeeById);
        //查出部门信息
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments", departments);
        return "emp/update";
    }

    //修改
    @RequestMapping("/emp/updateemp")
    public String updateEmp(Employee employee) {
        employeeDao.save(employee);
        return "redirect:/emps";
    }

    //    删除
    @RequestMapping("/emp/delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
        employeeDao.delete(id);
        return "redirect:/emps";
    }
}
